package examples

import "github.com/gin-gonic/gin"

type NPerson struct {
	ID   string `uri:"id" binding:"required,uuid"`
	Name string `uri:"name" binding:"required"`
}

func BindUriStart() {
	route := gin.Default()
	route.GET("/:name/:id", func(c *gin.Context) {
		var person NPerson

		// 方式一： 获取请求 uri 中参数
		name := c.Param("name")
		id := c.Param("id")

		person.ID = id
		person.Name = name

		// 方式二:
		// ShouldBindUri() : 将请求 uri 中参数绑定到 实体对象上
		if err := c.ShouldBindUri(&person); err != nil {
			c.JSON(400, gin.H{"msg": err})
			return
		}
		c.JSON(200, gin.H{"name": person.Name, "uuid": person.ID})
	})
	route.Run(":8088")
}
